package scau.jump.securitytrace.dataModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.table.DefaultTableModel;

import scau.jump.securitytrace.util.AddressConvertToVector;
import scau.jump.securitytrace.util.ConvertAdapter;

import com.securitytrace.model.Address;

public class AddressDataModel extends DefaultTableModel {

	private static final long serialVersionUID = 2241021746963165917L;
	private static final String[] addressColumns = new String[] { "地点名称", "描述" };
	
	private Map<Integer,Address> addressMap = new HashMap<Integer, Address>();
	private int addressCurrentPage;
	private int addressTotal;
	private ConvertAdapter<Address> addressConvertToVector;
	
	@SuppressWarnings("rawtypes")
	private Class[] types = new Class[] {java.lang.String.class, java.lang.String.class};
	private boolean[] canEdit = new boolean [] {false, false};
	
	public AddressDataModel() {
		addressConvertToVector = new AddressConvertToVector();
	}
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
        return canEdit [columnIndex];
    }
	@Override
	public Class<?> getColumnClass(int columnIndex) {
        return types[columnIndex];
    }
	
	/** 初始化数据 **/
	public void setDataVector(List<Address> addresses) {
		if(null != addresses && addresses.size() > 0) {
			for (int row = 0; row < addresses.size(); row++) {
				addressMap.put(row, addresses.get(row));
			}
			super.setDataVector(addressConvertToVector.convertToVector(addresses),
					addressConvertToVector.convertToVector(addressColumns));
		}else {
			super.setDataVector(new Object[][]{},addressColumns);
		}
	}

	/** 获取一行数据 **/
	public Address getRow(int row) {
		return addressMap.get(row);
	}

	/*** 更新一行数据 **/
	public void setValueAtRow(Address address, int row) {
		addressMap.put(row, address);
		this.setValueAt(address.getAddressName(), row, 0);
		this.setValueAt(address.getOtherInfo(), row, 1);
	}

	/***以下为get/set方法**/
	public Map<Integer, Address> getAddressMap() {
		return addressMap;
	}
	public void setAddressMap(Map<Integer, Address> addressMap) {
		this.addressMap = addressMap;
	}
	public int getAddressCurrentPage() {
		if(0 < addressCurrentPage) {
			return addressCurrentPage;
		}else {
			return 1;
		}
	}
	public void setAddressCurrentPage(int addressCurrentPage) {
		this.addressCurrentPage = addressCurrentPage;
	}
	public int getAddressTotal() {
		if(addressTotal > 0) {
			return addressTotal;
		}else {
			return 1;
		}
	}
	public void setAddressTotal(int addressTotal) {
		this.addressTotal = addressTotal;
	}
	public String getAddressPageInfo() {
		return "共 " + this.getAddressTotal() +" 页，当前为第 "+this.getAddressCurrentPage()+" 页";
	}
}
